import requests
import json


class PackageJsonApi:
    def __init__(self):
        pass

    def save(self, url, uuid, name):
        # Better approach: Define as Python dict and convert to JSON

        data_dict = {
            "uuid": uuid,
            "name": name,
            "icon": None,
            "version": "1",
            "tags": None,
            "software": None,
            "software_title": None,
            "package_version": 8,
            "feature_list": [
                2,
                5,
                7,
                8
            ],
            "description": None,
            "instruction": "",
            "use_latest_pip": False,
            "videoName": "",
            "startup": "main",
            "robot_type": "app",
            "activity_code": "",
            "flows": [
                {
                    "name": "main",
                    "filename": "main",
                    "kind": "Visual",
                    "opened": False,
                    "groupName": None
                },
                {
                    "name": "输入文本",
                    "filename": "process1",
                    "kind": "Visual",
                    "opened": False,
                    "groupName": None
                }
            ],
            "flow_groups": [],
            "variables": [],
            "external_dependencies": [],
            "internaldependencies": [],
            "selectordependencies": [],
            "internalautodependencies": [],
            "ipaasDependencies": [],
            "databook_columns": [],
            "authority": "use",
            "internalautoupgrade": False,
            "isbrief": False,
            "uia_type": "PC",
            "persist_databook": False,
            "encrypt_bot": False,
            "customItems": {
                "gifUrl": None,
                "videoUrl": "",
                "imageUrl": "",
                "imageName": ""
            },
            "terminalTags": "win,macos,linux"
        }

        # Convert to JSON string
        data = json.dumps(data_dict, ensure_ascii=False).encode('utf-8')

        # Set proper headers for JSON
        headers = {
            'Connection': 'keep-alive',
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
        }

        try:
            response = requests.put(url, data=data, headers=headers)
            print(f"package_json Status Code: {response.status_code}")
            print(f"package_json Response Text: {response.text}")
        except Exception as e:
            print(f"Error reading file: {e}")


if __name__ == '__main__':
    PackageJsonApi().save()
